from types import NoneType
import cv2
import numpy as np
from astropy.table.bst import MaxValue, MinValue

capture = cv2.VideoCapture(1)  # 打开笔记本内置摄像头
while (capture.isOpened()):  # 笔记本内置摄像头被打开后
    retval, image = capture.read()  # 从摄像头中实时读取视频
    img = image.copy()

    templet = cv2.imread('C:/Users/nic/Desktop/opencv/picture/ball_templet.jpg')
    # 获取templet的width和high
    w, h = templet.shape[0:2]

    gray = cv2.GaussianBlur(image, (7, 7), 0) #高斯滤波

    # methods are cv2.TM_CCOEFF,cv2.TM_CCOEFF_NORMED,cv2.TM_CCORR,cv2.TM_CCORR_NORMED,cv2.TM_SQDIFF and cv2.TM_SQDIFF_NORMED
    res = cv2.matchTemplate(gray, templet, cv2.TM_CCORR_NORMED)
    # 阈值，大于0.99的都会被标注
    threshold = 0.99
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):
        right_bottom = (pt[0] + w, pt[1] + h)
        cv2.rectangle(img, pt, right_bottom, (255, 0, 0), 1)


    cv2.imshow("Video", img)  # 在窗口中显示读取到的视频
    key = cv2.waitKey(1)  # 窗口的图像刷新时间为1毫秒
    if key == 27:  # 如果按下esc
        break
capture.release()  # 关闭笔记本内置摄像头
cv2.destroyAllWindows()  # 销毁显示摄像头视频的窗口


